/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.dao.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.sinodata.bsm.center.bean.EventPropHisBean;
import com.sinodata.bsm.center.dao.BaseDao;
import com.sinodata.bsm.center.dao.EventPropHisDao;
import com.sinodata.bsm.common.vo.EventPropHis;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author dongyushi
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-6-4 PM 10:43:13     dongyushi        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
@Repository
public class EventPropHisDaoImpl extends BaseDao implements EventPropHisDao {

    private final static Logger logger = LoggerFactory.getLogger(EventPropHisDaoImpl.class);

    /**
     * 通过事件ID查询事件产生时指标值
     */
    @Override
    public List<EventPropHisBean> getEventPropHisByEventId(Long eventId) {
        List<EventPropHisBean> resultList = new ArrayList<EventPropHisBean>();
        String format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat simple = new SimpleDateFormat(format);
        StringBuilder sb = new StringBuilder("select p.name");
        sb.append(",eph.id ");
        sb.append(",eph.event_id");
        sb.append(",eph.RES_ID");
        sb.append(",eph.prop_id");
        sb.append(",eph.prop_value");
        sb.append(",to_char(eph.PROP_COLLECT_TIME,'yyyy-MM-dd hh24:mi:ss')");
        sb.append(",to_char(eph.EVENT_OCCUR_TIME,'yyyy-MM-dd hh24:mi:ss')");
        sb.append(",p.data_unit");
        sb.append(" from event_prop_his eph,property p where eph.prop_id = p.id");
        sb.append(" and eph.event_id = ").append(eventId);
        try {
            Query query = getSession().createSQLQuery(sb.toString());
            List<Object[]> list = query.list();
            for (Object[] obj : list) {
                EventPropHisBean eventPropHisBean = new EventPropHisBean();
                eventPropHisBean.setPropName(String.valueOf(obj[0]));
                EventPropHis eventPropHis = new EventPropHis();
                eventPropHis.setId(Long.valueOf(String.valueOf(obj[1])));
                eventPropHis.setEventId(Long.valueOf(String.valueOf(obj[2])));
                eventPropHis.setResId(Long.valueOf(String.valueOf(obj[3])));
                eventPropHis.setPropId(Long.valueOf(String.valueOf(obj[4])));
                eventPropHis.setPropValue(obj[5] == null ? "" : String.valueOf(obj[5]));
                eventPropHis.setPropCollectTime(simple.parse(String.valueOf(obj[6])));
                eventPropHis.setEventOccurTime(simple.parse(String.valueOf(obj[7])));
                eventPropHisBean.setDataUnit(obj[8] == null ? "" : "(" + String.valueOf(obj[8]) + ")");
                eventPropHisBean.setEventPropHis(eventPropHis);
                resultList.add(eventPropHisBean);
            }
        } catch (Exception e) {
            logger.error("find event prop history by event id error", e);
        }
        return resultList;
    }

}